import {
    getToken
} from '@/util/auth'
const classNames = ['error', 'success', 'primary', 'info', 'error', 'warning'];
export default {
    methods: {
        showSuccess(msg) {
            this.$message({
                type: 'success',
                message: msg || '操作成功!'
            })
        },
        parseCellClassNameByValue(value) {
            return 'cell-' + classNames[value] || ''
        },
        parseCellClassNameByDicData(value, dicData) {
            for (let i = 0; i < dicData.length; i++) {
                const dicItem = dicData[i];
                if (value === dicItem.value && dicItem.type) {
                    return 'cell-' + dicItem.type;
                }
            }
            return '';
        },
        requestToken() {
            return {
                Authorization: getToken()
            }
        },
        alert(content, callback = () => {}) {
            this.$alert(content, '提示', {
                confirmButtonText: '确定',
                callback: callback
            });
        },
        showWarning(msg) {
            this.$message({
                type: 'warning',
                message: msg
            })
        },
        showNotify(msg) {
            this.$notify({
                title: '成功',
                message: msg || '操作成功',
                type: 'success',
                duration: 2000
            })
        },
        showError(msg) {
            this.$message({
                type: 'error',
                message: msg
            })
        },
        showConfirm(msg, sureCallback) {
            this.$confirm(msg, '提醒', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(sureCallback).catch(() => {})
        },
        openWin(newUrl) {
            let iframe = document.createElement('iframe')
            iframe.style.display = 'none'
            let form = document.createElement('form')
            form.action = newUrl
            form.method = 'get'
            form.target = '_self'
            iframe.appendChild(form)
            document.body.appendChild(iframe)
            form.submit()
            setTimeout(() => {
                iframe.parentElement.removeChild(iframe)
            }, 100)
        },
        exportExcelWithForm(newUrl, queryParams) {
            let iframe = document.createElement('iframe')
            iframe.style.display = 'none'
            let form = document.createElement('form')
            form.action = newUrl
            form.method = 'post'
            form.target = '_self'
            if (queryParams) {
                Object.keys(queryParams).forEach(field => {
                    const inputField = document.createElement('input')
                    inputField.name = field
                    inputField.type = 'hidden'
                    inputField.value = queryParams[field]
                    form.appendChild(inputField)
                })
            }
            iframe.appendChild(form)
            document.body.appendChild(iframe)
            form.submit()
            setTimeout(() => {
                iframe.parentElement.removeChild(iframe)
            }, 100)
        }
    }
}
